package com.module.my.model

import cn.leancloud.LCObject
import cn.leancloud.LCQuery
import com.module.app.base.BaseViewModel
import com.module.app.bean.ListViewBean
import com.module.base.ext.request
import com.module.frame.ext.postEvent
import com.module.my.bean.ProblemBean
import com.module.third.learcloud.lcQuery

class ProblemViewModel : BaseViewModel() {

    fun getData(isRefresh: Boolean, page_no: Int, page_size: Int) {
        request({
            val tClass = ProblemBean::class.java
            val query = LCQuery<LCObject>(tClass.simpleName)
            //升序
            query.orderByAscending("sort")
            query.limit(page_size)
            query.skip(page_no * page_size)
            lcQuery(query, tClass)
        }, {
            if (it.isNullOrEmpty()) {
                postEvent(listFlow, ListViewBean(isRefresh, null, true))
            } else {
                val isNoMore = it.size < page_size
                postEvent(listFlow, ListViewBean(isRefresh, it, isNoMore))
            }
        }, {
            postEvent(errorFlow, it)
        }, false)
    }
}